<?php

/**
 * @author juanma
 * @copyright 2012
 */


class Sgit_Graficos_Lecturas extends Sgit_Graficos_Parent
{
    


     private function getData()
     {
        
        $this->id=     $this->request->getParam('id');
        $this->tipo=   $this->request->getParam('tipo','consumo');

        $m_lecturas = $this->loadModel('Lecturas');
        $m_contadores = $this->loadModel('Contadores');
        
        $contador = $m_contadores->getContador($this->id);
        $lecturas = $m_lecturas->getConsumos($this->id);

        $lecturas=@array_reverse($lecturas,true);
       

        switch($this->tipo){
            case 'lectura':
                $this->key='lectura';
                $this->titulo="Consumo Kwh"; 
                $this->ytitle='kWh';
                $this->format='%d';
                break;
            case 'consumo':
                $this->key='consumo';
                $this->titulo="Consumo Kwh"; 
                $this->ytitle='kWh';
                $this->format='%d';
                break;
            case 'potencia':
                $this->key='potencia'; 
                $this->titulo="Potencia Kw";
                $this->ytitle='kW';
                $this->format='%0.2f';
                break;
            case 'diff':
                $this->key='incremento_potencia';
                $this->titulo="Dif Potencia %"; 
                $this->ytitle='%';
                $this->format='%0.2f';
                break;
            default:
                $this->key='consumo';
                $this->titulo="Consumo Kwh"; 
                $this->ytitle='kWh';
                $this->format='%d';
                break;
        }
       
       

        $this->title=$this->titulo.' '.$contador['codigo'];
        $this->subtitle=$contador['id_proyecto'];


        $fecha=new Zend_Date();
        
        foreach((array)$lecturas as $j => $v)
        {
            $fecha->set($v['date_consumo']);
            $this->label[]=$fecha->get('MMM-yy');
            $this->values[$this->key][]=($v[$this->key]) ? $v[$this->key] : '';
            
            $text1[]=$fecha->get('MMM-yy');
            $text2[]=$this->view->numEs($v['lectura'],0);
        }
    
    
        $text1=array_reverse($text1);
        $this->text1=implode(PHP_EOL,$text1);
        $text2=array_reverse($text2);
        $this->text2=implode(PHP_EOL,$text2);
        
    }


     public function jpGrapgh()
     {
        
        $this->getData();
        
        $this->loadJpgraph('bar');
        
        $step=$this->_step(count($this->values[$this->key]),$this->width);
        
        $graph = $this->_graph($this->width,$this->height);
        $graph->title->Set(utf8_decode($this->title));
        $graph->subtitle->Set(utf8_decode($this->subtitle));
        $graph->SetMargin(75, 150, 30, 80);
        $graph->yaxis->SetTitle($this->ytitle,'middle'); 
        $graph->xaxis->SetTickLabels($this->label);
        $graph->xaxis->SetTextLabelInterval($step);


        // Create the bar pot
        $barplot= $this->_barplot($this->values[$this->key]);
        $barplot->value->Show();
        $barplot->value->SetFont(FF_ARIAL,FS_BOLD,8);
        $barplot->value->SetAngle(90);
        $barplot->value->SetFormat($this->format);
        $barplot->SetValuePos('center');

        $graph->Add($barplot);
         
         
        $txt = new Text($this->text1);
        $txt->SetPos($this->width-145, 45);
        $txt->SetFont(FF_ARIAL,FS_ITALIC,8);
        
        $txt2 = new Text($this->text2);
        $txt2->SetPos($this->width-90, 45);
        $txt2->SetFont(FF_ARIAL,FS_NORMAL,8);
        
        $txt3 = new Text('Lecturas Kwh');
        $txt3->SetPos($this->width-145, 30);
        $txt3->SetFont(FF_ARIAL,FS_ITALIC,10);
        
        $graph->AddText($txt); 
        $graph->AddText($txt2); 
        $graph->AddText($txt3);  
         
        $graph->Stroke();
     }
     
     private function googleVisualization()
     {
        
     }
     

    
}